package com.control;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.emp.database.LoginOperate;

/**
 * Servlet implementation class checkLogin
 */
@WebServlet(name="checkLogin",urlPatterns={"/"},initParams={@WebInitParam(name="encoding",value="utf-8")})
public class checkLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
     ServletConfig config; 
     String encoding;
    /**
     * @see HttpServlet#HttpServlet()
     */
   
    public checkLogin() {    
        super();
        // TODO Auto-generated constructor stub
    }
    
   public void init(ServletConfig config) throws ServletException {
    	this.config = config;
    	encoding = config.getInitParameter("encoding");
    	System.out.println("&&&&&&&&"+encoding);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取application对象的实例
		ServletContext application=config.getServletContext();
		//获取session的实例
		HttpSession session= request.getSession();
		//重新指定提交的编码格式
		request.setCharacterEncoding(encoding);
		String username =request.getParameter("username");
		System.out.print(username);
		
		String password =request.getParameter("password");
		String ck = request.getParameter("checkbox");
		
		LoginOperate lo = new LoginOperate();
		int flag = lo.checkLogin(username,password);
		
		if(flag == 1){
			if(ck!=null&&ck.equals("on")){
				Cookie c1=new Cookie("username",username);
				Cookie c2=new Cookie("password",password);
				//设置Cookie时间
				c1.setMaxAge(24*3600);
				c2.setMaxAge(24*3600);
				//将Cookie写入客户端磁盘
				response.addCookie(c1);
				response.addCookie(c2);
			}
			
			Integer it=(Integer)application.getAttribute("count");
			if(it == null){
				application.setAttribute("count", 1);
			}else{
				it+=1;
				application.setAttribute("count", it);
			}
			session.setAttribute("uname",username);
			request.getRequestDispatcher("manager/manager.jsp").forward(request, response);
		}else{
			
			request.setAttribute("message", "用户名或密码错误");						
			request.getRequestDispatcher("Login.jsp").forward(request, response);
		}
		
	}

}
